package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCollect;
import com.cskaoyan.bean.MarketCollectExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.WxCcollectAddorDeleteBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCollectListVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: Tao
 * @time: 2023/1/1 14:53
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * 完成商品收藏的添加或者删除操作
     *
     * @param addordeleteBo
     */
    @Override
    public void addordelete(WxCcollectAddorDeleteBo addordeleteBo) {
        //分为二种情况，一种情况第一次添加收藏，表中没有此商品收藏数据只执行插入操作，
        // 一种情况有此商品的收藏信息，将delete字段取反，再修改。

        //判断是否有此商品收藏数据
        Integer goodsId = addordeleteBo.getValueId();
        //实例化对象
        MarketCollectExample example = new MarketCollectExample();
        //获取当前用户的id
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        //增加条件查询
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(goodsId);
        criteria.andUserIdEqualTo(userId);
        //获得收藏数据
        List<MarketCollect> marketCollects = collectMapper.selectByExample(example);
        //如果没找到（marketCollects的大小是0）说明第一次添加收藏，此时收藏表中没有此商品收藏数据只执行插入操作
        if (marketCollects.size() == 0) {
            MarketCollect marketCollect = new MarketCollect();
            marketCollect.setUserId(userId);
            marketCollect.setValueId(goodsId);
            marketCollect.setType(addordeleteBo.getType());
            marketCollect.setAddTime(new Date());
            marketCollect.setUpdateTime(new Date());
            marketCollect.setDeleted(false);
            collectMapper.insertSelective(marketCollect);
        } else {
            //有此商品的收藏信息，将delete字段取反，再修改。
            MarketCollect marketCollect = marketCollects.get(0);
            Boolean deleted = marketCollect.getDeleted();
            //delete字段取反
            marketCollect.setDeleted(!deleted);
            marketCollect.setUpdateTime(new Date());
            //修改
            collectMapper.updateByPrimaryKeySelective(marketCollect);
        }
    }

    /**
     * 查看收藏列表
     *
     * @param info
     * @param type
     * @return
     */
    @Override
    public CommonData<WxCollectListVo> gitList(BasePageInfo info, Byte type) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //实例化对象
        MarketCollectExample example = new MarketCollectExample();
        //先排序
        example.setOrderByClause("add_time" + " " + "desc");
        //增加条件查询
        MarketCollectExample.Criteria criteria = example.createCriteria();
        // 查询逻辑存在,表中deleted字段值为1是删除状态
        criteria.andDeletedEqualTo(false);
        //获取当前的用户id
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        //只查询当前用户的收藏
        criteria.andUserIdEqualTo(userId);
        //获取收藏列表
        List<MarketCollect> marketCollects = collectMapper.selectByExample(example);
        //存放封装后的Vo信息
        ArrayList<WxCollectListVo> collectAndGoodsListVos = new ArrayList<>();
        for (MarketCollect marketCollect : marketCollects) {
            //查询每一收藏的商品信息
            Integer goodsId = marketCollect.getValueId();
            MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
            //封装
            WxCollectListVo wxCollectListVo = new WxCollectListVo(goods.getBrief(), goods.getPicUrl(), goods.getId(), goods.getName(), marketCollect.getId(),
                    marketCollect.getType(), goods.getRetailPrice());
            collectAndGoodsListVos.add(wxCollectListVo);
        }
        //封装
        PageInfo<WxCollectListVo> pageInfo = new PageInfo<>(collectAndGoodsListVos);
        return CommonData.data(pageInfo);
    }
}
